package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理")
@Slf4j
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor// 自动注入所有final字段/代替Autowired
public class UserController {

    private final IUserService userService;

    @ApiOperation("新增用户")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO dto) {
        log.info("新增用户");
        User user = BeanUtil.copyProperties(dto, User.class);
        userService.save(user);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public void deleteUser(@RequestParam Long id) {
        log.info("删除用户");
        userService.removeById(id);
    }

    @ApiOperation("查询用户")
    @GetMapping("/{id}")
    public UserVO queryById(@PathVariable("id") Long id) {
//        log.info("查询用户");
//        User user = userService.getById(id);
//        return BeanUtil.copyProperties(user, UserVO.class);
        return userService.queryUserAndAddressById(id);
    }

    @ApiOperation("根据id批量查询")
    @GetMapping
    public List<UserVO> queryByIds(@RequestParam("ids") List<Long> ids) {
//        log.info("根据id批量查询");
//        List<User> users = userService.listByIds(ids);
//        return BeanUtil.copyToList(users, UserVO.class);
        return userService.queryUserAndAddressByIds(ids);
    }

    /**
     * 根据用户Id、金额扣减用户的余额
     * @param id 用户id
     * @param amount 金额
     */
    @ApiOperation("根据用户Id、金额扣减用户的余额")
    @PutMapping("/{id}/deduction/{amount}")
    public void updateBalanceById(@PathVariable("id") Long id, @PathVariable("amount") Integer amount) {
        userService.deductBalance(id, amount);
    }

    @ApiOperation("根据条件查询用户列表")
    @PostMapping("/list")
    public List<UserVO> queryUserList(@RequestBody UserQuery dto) {
        log.info("根据条件查询用户列表");
        String username = dto.getName();
        Integer status = dto.getStatus();
        Integer minBalance = dto.getMinBalance();
        Integer maxBalance = dto.getMaxBalance();

//        LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda()
//                .like(StrUtil.isBlankIfStr(username), User::getUsername, username)
//                .eq(status != null, User::getStatus, status)
//                .ge(minBalance != null, User::getBalance, minBalance)
//                .le(maxBalance != null, User::getBalance, maxBalance);
//        List userList = userService.list(queryWrapper);

        List userList = userService.lambdaQuery()
                .like(StrUtil.isNotBlank(username), User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();

        return BeanUtil.copyToList(userList, UserVO.class);
    }
}
